//
//  CSLoginViewController.m
//  ChinaShip
//
//  Created by Yale on 2019/2/22.
//  Copyright © 2019年 ChinaShip. All rights reserved.
//

#import "CSLoginViewController.h"

@interface CSLoginViewController ()
{
    IBOutlet UITextField *loginTextField,*pwdTextField;
    IBOutlet NSLayoutConstraint *hLayoutConstraint;
}
@end

@implementation CSLoginViewController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self hiddenNavBar];
    if (isIPhonex) {
        hLayoutConstraint.constant = 40;
        [self.view layoutIfNeeded];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (IBAction)popVCAction:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)loginAction:(id)sender{
    if (loginTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"Account cannot be empty"];
        return;
    }
    
    if (pwdTextField.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"Password can not be blank"];
        return;
    }
    [SVProgressHUD show];
    [self performSelector:@selector(exitPush) withObject:nil afterDelay:1];
}

- (void)exitPush{
    BOOL isLogin = NO;
    NSMutableArray *dataArray = [CSTool getAllUsers];
    for (NSDictionary *dic in dataArray) {
        NSString *user = [dic objectForKey:@"user"];
        NSString *pwd = [dic objectForKey:@"pwd"];
        if ([user isEqualToString:loginTextField.text]&&[pwd isEqualToString:pwdTextField.text]) {
            isLogin = YES;
            [CSTool saveLoginState];
            [SVProgressHUD showSuccessWithStatus:@"login successful"];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }
    if (!isLogin) {
        [SVProgressHUD showErrorWithStatus:@"Incorrect username or password"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
